<?php
/**
 * TomatoCMS
 *
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 *
 * @copyright    Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license        http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version     $Id: product.php 2870 2010-05-17 03:04:13Z huuphuoc $
 * @since        2.0.5
 */

class Shop_Models_Dao_Pdo_Mysql_ProductAttribute extends Tomato_Model_Dao
    implements Shop_Models_Interface_ProductAttribute
{
    public function convert($entity)
    {
        return new Shop_Models_ProductAttribute($entity);
    }
    public function duplicate($product_attribute)
    {
        $data = array(
            'product_id'            => $product_attribute->product_id,
            'attribute_type_id'        => $product_attribute->attribute_type_id,
            'value_type_id_chain'    => $product_attribute->value_type_id_chain
        );
        $this->_conn->insert($this->_prefix.'shop_product_attribute', $data);
        return $this->_conn->lastInsertId($this->_prefix.'shop_product_attribute');
    }
    public function add($product_attribute)
    {
        $data = array(
            'product_id'            => $product_attribute->product_id,
            'attribute_type_id'        => $product_attribute->attribute_type_id,
            'value_type_id_chain'    => ','
        );
        $this->_conn->insert($this->_prefix.'shop_product_attribute', $data);
        return $this->_conn->lastInsertId($this->_prefix.'shop_product_attribute');
    }
    public function delete($product_id, $attribute_type_id)
    {
        $where[] = 'product_id = '.$this->_conn->quote($product_id);
        $where[] = 'attribute_type_id = '.$this->_conn->quote($attribute_type_id);
        return $this->_conn->delete($this->_prefix.'shop_product_attribute', $where);
    }
    public function getAttributeTypeIdByProductId($product_id){
        $select = $this->_conn
                        ->select()
                        ->from(array('pt' => $this->_prefix.'shop_product_attribute'))
                        ->where("product_id = ?",$product_id)
                        ;
        $rowset = $select->query()->fetchAll();
        return  new Tomato_Model_RecordSet($rowset, $this);
    }
    public function getAttributeTypeByAttributeTypeId($attribute_type_id){
        $select = $this->_conn
                        ->select()
                        ->from(array('pt' => $this->_prefix.'shop_attribute_type'))
                        ->where("attribute_type_id = ?",$attribute_type_id)
                        ;
        $row = $select->query()->fetchAll();
        $attribute_type = new Tomato_Model_RecordSet($row, $this);
        return (count($attribute_type) == 0) ? null : $attribute_type[0];
    }
    public function deleteValueType($id)
    {
        $where[] = 'value_type_id = '.$this->_conn->quote($id);
        return $this->_conn->delete($this->_prefix.'shop_value_type', $where);
    }

    public function deleteValueContent($id)
    {
        $where[] = 'value_type_id = '.$this->_conn->quote($id);
        return $this->_conn->delete($this->_prefix.'shop_value_content', $where);
    }
    public function deleteProductAttribute($product_id)
    {
        $where[] = 'product_id = '.$this->_conn->quote($product_id);
        return $this->_conn->delete($this->_prefix.'shop_product_attribute', $where);
    }
}